package com.example.administrator.orchardeveryday.fragments;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.administrator.orchardeveryday.R;
import com.example.administrator.orchardeveryday.adapter.FruitPagerAdapter;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 */
public class FruitFragment extends Fragment implements RadioGroup.OnCheckedChangeListener,ViewPager.OnPageChangeListener{
    private ViewPager viewPager;
    private FruitPagerAdapter pagerAdapter;
    private ArrayList<Fragment> fragments=new ArrayList<>();
    private RadioGroup radioGroup;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_fruit,null);
        initView(view);
        return view;
    }

    private void initView(View view) {
        viewPager= (ViewPager) view.findViewById(R.id.fragment_fruit_viewpager);
        radioGroup= (RadioGroup) view.findViewById(R.id.fragment_fruit_radiogroup);
        fragments.add(new BaikeFragment());
        fragments.add(new FruitFriendsFragment());
        pagerAdapter=new FruitPagerAdapter(getChildFragmentManager(),fragments);
        viewPager.setAdapter(pagerAdapter);
        viewPager.setOnPageChangeListener(this);
        ((RadioButton) radioGroup.getChildAt(0)).setChecked(true);
        radioGroup.setOnCheckedChangeListener(this);
    }


    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId)
        {
            case R.id.fragment_fruit_baike_rbt:
                viewPager.setCurrentItem(0);
                break;
            case R.id.fragment_fruit_friends_rbt:
                viewPager.setCurrentItem(1);
                break;
        }
    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {
        ((RadioButton)radioGroup.getChildAt(i)).setChecked(true);
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }
}
